// pages/user_opt/vehicleEditor/vehicleEditor.js
const VEH = require("../../interfaces/vehicles")
const IFS = require("../../interfaces/interface")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode: "",
    currentID: "",
    currentName: "",
    currentPower: '',
    currentCapacity: '',
    t_name: "",
    t_power: "",
    t_cap: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      action,
      id,
      name,
      curp,
      capp
    } = options;
    if (action == 'edit') {
      this.setData({
        mode: 'edit',
        currentID: id,
        currentName: name,
        currentPower: curp,
        currentCapacity: capp,
      })
    } else if (action == "add") {
      this.setData({
        mode: 'add'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  inputName(e) {
    this.setData({
      t_name: e.detail.value.trim(),
    });
  },

  inputCurrentPower(e) {
    this.setData({
      t_power: e.detail.value.trim(),
    });
  },

  inputCapacity(e) {
    // console.log(e)
    this.setData({
      t_cap: e.detail.value.trim(),
    });
  },

  cancel() {
    // 清空输入框数据
    this.setData({
      t_name: '',
      t_power: '',
      t_cap: '',
    });
  },

  async submit() {
    if (this.data.mode == "edit") {
      if (this.data.t_name == "") {
        this.setData({
          t_name: this.data.currentName
        })
      }
      if (this.data.t_power == "") {
        this.setData({
          t_power: this.data.currentPower
        })
      }
      if (this.data.t_cap == "") {
        this.setData({
          t_cap: this.data.currentCapacity
        })
      }
      console.log("修改车辆信息：", {
        car_id: this.data.currentID,
        name: this.data.t_name,
        capacity: this.data.t_cap,
        power:this.data.t_power
      })
      // 执行提交操作
      let res = await (await IFS.updateVehicle(this.data.currentID, this.data.t_name, this.data.t_cap, this.data.t_power))
      /*检测状态*/
      if (res.status == "success") {
        wx.navigateBack()
        wx.showToast({
          title: '修改成功！', // 对话框的标题
        });
      } else {
        console.log(res)
        wx.showModal({
          title: "修改失败",
          content: res.msg,
        })
      }
    } else if (this.data.mode == "add") {
      if (!this.data.t_name || !this.data.t_power || !this.data.t_cap) {
        wx.showToast({
          title: 'Please fill in all fields',
          icon: 'none',
        });
        return;
      }
      // 执行提交操
      console.log("插入车辆信息：", {
        name: this.data.t_name,
        capacity: this.data.t_cap,
        power:this.data.t_power
      })
      let res = await (await IFS.addVehicle(this.data.t_name, this.data.t_cap, this.data.t_power))
      /*检测状态*/
      if (res.status == "success") {
        wx.navigateBack();
        wx.showToast({
          title: '车辆添加成功',
        })
      } else {
        console.log(res)
        wx.showToast({
          title: "添加失败",
          icon: "error"
        })
      }
      // 清空输入框数据
      this.setData({
        name: '',
        currentPower: '',
        capacity: '',
      });
    }
  }
})